using System;
using System.Collections.Generic;
using System.Text;

namespace NBusiness.CodeDom
{
	[Serializable]
	public abstract class EntityFamily : EntityCompileUnit, IFamilyUnit
	{
		#region Member fields and properties
		private string _name = null;
		public virtual string Name
		{
            get { return _name; }
            set { _name = value; }
		}

        public string Fullname
        {
            get
            {
                string name = "";
                if (Parent != null && !string.IsNullOrEmpty((Parent as IFamilyUnit).Fullname))
                {
                    name = (Parent as IFamilyUnit).Fullname + ".";
                }

                return name + Name;
            }
        }

        private Dictionary<string, EntityFamily> _families = new Dictionary<string, EntityFamily>();
        public virtual Dictionary<string, EntityFamily> Families
        {
            get { return _families; }
        }

		private List<Entity> _entities = new List<Entity>();
		public virtual List<Entity> Entities
		{
			get { return _entities; }
		}

        private IHasParent _parent = null;
        public IHasParent Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public EntityRoot Root
        {
            get
            {
                IHasParent root = Parent;
                while (root.Parent != null) root = root.Parent;

                return root as EntityRoot;
            }
        }
		#endregion

		#region object overrides
		public override string ToString()
		{
            return _name;
		}
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
		public override bool Equals(object obj)
		{
			if (!(obj is EntityFamily)) return false;

			return Name.Equals((obj as EntityFamily).Name);
		}
		#endregion
	}
}
